package com.hex.liyue.usermodule.presenter;

import com.hex.liyue.commonmodule.listener.UploadFileListener;
import com.hex.liyue.commonmodule.manager.HttpManager;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.net.ResponseListener;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.OssUtils;
import com.hex.liyue.usermodule.bean.MyPhotoBean;
import com.hex.liyue.usermodule.bean.UpdateMyPhotoBean;

import java.io.File;
import java.util.List;

/**
 * Created by $cmm on 2016/6/21.
 * <p/>
 * 我的照片的业务类
 */
public class MyPhototsPresenter {
    private IMyPhotosView iMyPhotosView;

    public MyPhototsPresenter(IMyPhotosView iMyPhotosView) {
        this.iMyPhotosView = iMyPhotosView;
    }

    public void getAddPhoto() {
        iMyPhotosView.showLod("加载中...");
        HttpManager.getInstance().getAddPhoto(UserManager.getUserId(), new ResponseListener<MyPhotoBean>() {
                    @Override
                    public void onSuccess(MyPhotoBean myPhotoBean) {
                        iMyPhotosView.closeLod();
                        iMyPhotosView.setAdapter(myPhotoBean);

                    }

                    @Override
                    public void onFailure(String responseresult) {
                        iMyPhotosView.closeLod();
                        iMyPhotosView.showT(responseresult);
                    }
                }

        );
    }

    int updateNum = 0;//需要上传几张
    int updateSuccessNum = 0;//上传成功几张
    String[] paths;

    public void getUpdatePhoto(List<MyPhotoBean.DataBean.UrlsBean> list) {
        updateNum = 0;
        updateSuccessNum = 0;
        paths = null;

        //所有图片集合 需要过滤
        iMyPhotosView.showLod("加载中...");
        for (int i = 0; i < list.size(); i++) {
            MyPhotoBean.DataBean.UrlsBean urlsBean = list.get(i);
            if (urlsBean.file_id != null) {
                //说明是网络图片  不需要上传了
                continue;
            } else {
                updateNum++;
                File localFile = new File(urlsBean.url);
                String key = OssUtils.getInstance().objectKeyUserPhoto + UserManager.getUserId() + "_" + localFile.getName();
                OssUtils.getInstance().uploadfile(key, localFile.getAbsolutePath(), new UploadFileListener() {
                    @Override
                    public void onSucess(String path) {
                        paths[updateSuccessNum] = path;
                        updateSuccessNum++;
                        L.d("WJ", "成功=" + paths);
                        updatePhoto();
                    }

                    @Override
                    public void onFailure() {

                    }

                    @Override
                    public void onProgress(long progress) {

                    }
                });
            }
        }
        paths = new String[updateNum];
        L.d("WJ", "需要上传几张=" + updateNum);
    }

    private void updatePhoto() {
        //上传图片到自己服务器
        L.d("WJ", "上传图片到自己服务器");
        if (updateNum == updateSuccessNum) {
            //成功上传所有图片
            HttpManager.getInstance().getUpdatePhoto(UserManager.getUserId(), paths, new ResponseListener<UpdateMyPhotoBean>() {
                @Override
                public void onSuccess(UpdateMyPhotoBean updateMyPhotoBean) {
                    iMyPhotosView.closeLod();
                    iMyPhotosView.setFinsh();
//                    getAddPhoto();
//                    iMyPhotosView.closeCurrentActivity(updateMyPhotoBean.data.size());
                }

                @Override
                public void onFailure(String responseresult) {
                    iMyPhotosView.closeLod();
                    iMyPhotosView.showT(responseresult);
                }
            });
        }

    }
}
